package condominio.servlet.reserva;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import condominio.excepcion.DAOExcepcion;
import condominio.modelo.Usuario;
import condominio.negocio.GestionReservas;

/**
 * Servlet implementation class ReservaInsertarServlet
 */
@WebServlet("/ReservaInsertarServlet")
public class ReservaInsertarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReservaInsertarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		

		HttpSession session = request.getSession();
		Usuario usuario_actual = (Usuario)session.getAttribute("USUARIO_ACTUAL");
		
		String residente = usuario_actual.getResidente();
		byte espacioreservado = Byte.parseByte(request.getParameter("espacio"));
		byte motivoreserva = Byte.parseByte(request.getParameter("motivo"));
		String comentario = request.getParameter("comentario");
		byte hora = Byte.parseByte(request.getParameter("hora"));
		
		SimpleDateFormat parse = new SimpleDateFormat("dd-MM-yyyy");
		
		String strfechareserva=request.getParameter("fecha");
		java.util.Date datefechareserva=null;
		try {
			datefechareserva = parse.parse(strfechareserva);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		java.sql.Date fechareserva = new java.sql.Date(datefechareserva.getTime());
		
		
		GestionReservas negocio = new GestionReservas();
		try {
			negocio.insertar(residente, espacioreservado, fechareserva, hora, motivoreserva, comentario);
			//response.sendRedirect(request.getContextPath()	+ "/Reservas.jsp");
			request.setAttribute("mensaje", "Reserva ingresada correctamente");
			request.setAttribute("nuevo", "ReservaNuevo.jsp");
			request.setAttribute("nuevovalue", "Nueva Reserva");
			request.setAttribute("listar", "Reservas.jsp");
			request.setAttribute("listarvalue", "Ver Reservas");
			RequestDispatcher rd = request.getRequestDispatcher("Exito.jsp");
			rd.forward(request, response);
		} catch (DAOExcepcion e) {
			request.setAttribute("mensaje", e.getMessage());
			RequestDispatcher rd = request.getRequestDispatcher("Error.jsp");
			rd.forward(request, response);
		}
		
		
	}

}
